package com.example.testapplication;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

import androidx.annotation.Nullable;

import com.example.testapplication.adapter.ListViewAdapter;
import com.example.testapplication.entity.Food;

import java.util.ArrayList;
import java.util.List;

public class FoodListActivity extends ApplicationActivity{
    ListView listView;
    List<Food> foodList = new ArrayList<>();
    List<Food> selectedList = new ArrayList<>();
    EditText editText;
    Button button;
    ListViewAdapter listViewAdapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_food_list);
        listView = findViewById(R.id.list_view);
        button = findViewById(R.id.buttonfood);
        editText = findViewById(R.id.edit_text);
        foodList.add(new Food("麻辣香锅", 55));
        foodList.add(new Food("水煮鱼", 48));
        foodList.add(new Food("麻辣火锅", 80));
        foodList.add(new Food("清蒸鲈鱼", 68));
        foodList.add(new Food("桂林米粉", 15));
        foodList.add(new Food("上汤娃娃菜", 28));
        foodList.add(new Food("红烧肉", 60));
        foodList.add(new Food("木须肉", 40));
        foodList.add(new Food("酸菜牛肉面", 35));
        foodList.add(new Food("西芹炒百合", 38));
        foodList.add(new Food("酸辣汤", 40));
        foodList.add(new Food("酸辣汤", 40));
        foodList.add(new Food("酸辣汤", 40));
        foodList.add(new Food("酸辣汤", 40));
        selectedList.addAll(foodList);
        listViewAdapter = new ListViewAdapter(selectedList,this);
        listView.setAdapter(listViewAdapter);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                try {
                    int price = Integer.parseInt(editText.getText().toString());
                    selectedList.clear();
                    for (Food unit:foodList){
                        if(unit.getPrice() <= price){
                            selectedList.add(unit);
                        }
                    }
                    listViewAdapter.notifyDataSetChanged();
                }catch (Exception e){
                    Toast.makeText(FoodListActivity.this,"请输入正确价格",Toast.LENGTH_SHORT).show();
                }
            }
        });





    }
}
